/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dlshouwen.wzgl.content.controller;

import com.dlshouwen.core.base.config.CONFIG;
import com.dlshouwen.core.base.extra.grid.model.Pager;
import com.dlshouwen.core.base.extra.grid.utils.PagerPropertyUtils;
import com.dlshouwen.core.base.model.AjaxResponse;
import com.dlshouwen.core.base.model.OperationType;
import com.dlshouwen.core.base.model.SessionUser;
import com.dlshouwen.core.base.utils.AttributeUtils;
import com.dlshouwen.core.base.utils.GUID;
import com.dlshouwen.core.base.utils.LogUtils;
import com.dlshouwen.wzgl.album.dao.AlbumDao;
import com.dlshouwen.wzgl.album.model.Album;
import com.dlshouwen.wzgl.channel.dao.ChannelDao;
import com.dlshouwen.wzgl.channel.model.Channel;
import com.dlshouwen.wzgl.content.dao.ArticleDao;
import com.dlshouwen.wzgl.content.model.Article;
import com.dlshouwen.wzgl.picture.model.Picture;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

/**
 * 文章控制类
 *
 * @author cuixubin
 */
@Controller
@RequestMapping("/wzgl/article")
public class ArticleController {
	/**
	 * 功能根路径
	 */
	private String basePath = "wzgl/article/";

	/**
	 * 数据操作对象
	 */
	private ArticleDao dao;

	/**
	 * 数据操作对象
	 */
	private AlbumDao albumDao;

	/**
	 * 注入数据操作对象
	 *
	 * @param dao
	 *            数据操作对象
	 */
	@Resource(name = "articleDao")
	public void setDao(ArticleDao dao) {
		this.dao = dao;
	}

	/**
	 * 数据操作对象
	 */
	@Resource(name = "ChannelDao")
	private ChannelDao channelDao;

	public void setChannelDao(ChannelDao channelDao) {
		this.channelDao = channelDao;
	}

	/**
	 * 注入数据操作对象
	 *
	 * @param dao
	 *            数据操作对象
	 */
	@Resource(name = "albumDao")
	public void setAlbumDao(AlbumDao albumDao) {
		this.albumDao = albumDao;
	}

	/**
	 * 跳转到文章管理主页面
	 *
	 * @param request
	 *            请求对象
	 * @return basePath + 'articleList'
	 * @throws Exception
	 *             抛出全部异常
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String goArticlePage(HttpServletRequest request) throws Exception {

		// 执行查询栏目
		List<Map<String, Object>> channelList = channelDao.getChannelList();
		Map<String, Object> map1 = new HashMap<>();
		for (Map<String, Object> map : channelList) {
			String channel_id = (String) map.get("channel_id");
			String channel_name = (String) map.get("channel_name");
			map1.put(channel_id, channel_name);
		}
		request.setAttribute("channelList", JSONObject.fromObject(map1).toString());
		String selChannelId = request.getParameter("selChannelId");
		request.setAttribute("selChannelId", selChannelId);
		return basePath + "articleList";
	}

	/**
	 * 获取文章列表数据
	 *
	 * @param gridPager
	 *            表格分页信息
	 * @param request
	 *            请求对象
	 * @param response
	 *            响应对象
	 * @throws Exception
	 *             抛出全部异常
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public void getArticleList(String gridPager, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 映射Pager对象
		Pager pager = PagerPropertyUtils.copy(JSONObject.fromObject(gridPager));
		// 获取文章列表数据
		dao.getArticleList(pager, request, response);
		// 记录操作日志
		LogUtils.updateOperationLog(request, OperationType.SEARCH, "获取文章管理页面中的文章列表数据");
	}

	/**
	 * 跳转到新增文章页面
	 *
	 * @param model
	 *            反馈对象
	 * @param request
	 *            请求对象
	 * @return basePath + 'addArticle'
	 * @throws Exception
	 *             抛出全部异常
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String addArticle(Model model, HttpServletRequest request) throws Exception {
		// 新增文章对象
		Article article = new Article();

		// 获取当前用户
		SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER);
		String publisher = sessionUser == null ? "" : sessionUser.getUser_name();

		article.setArticle_id(new GUID().toString());
		// 设置默认启用
		// 启用
		article.setStatus("1");
		// 不置顶
		article.setTopset("0");
		// 发布人姓名
		article.setPublisher(publisher);
		// 发布时间
		article.setPublish_time(new Date());

		// 传递文章对象到前台
		model.addAttribute("article", article);

		return basePath + "addArticle";
	}

	/**
	 * 执行新增文章
	 *
	 * @param article
	 *            文章对象
	 * @param bindingResult
	 *            绑定结果
	 * @param request
	 *            请求对象
	 * @return ajax响应内容
	 * @throws Exception
	 *             抛出全部异常
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse addArticle(@Valid Article article, BindingResult bindingResult, HttpServletRequest request)
			throws Exception {
		// 定义AJAX响应回执
		AjaxResponse ajaxResponse = new AjaxResponse();
		// 如果发现错误则拼接错误信息执行回执
		if (bindingResult.hasErrors()) {
			ajaxResponse.bindingResultHandler(bindingResult);
			return ajaxResponse;
		}
		// 获取当前用户、当前时间
		SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER);
		String userId = sessionUser.getUser_id();
		Date nowDate = new Date();
		// 设置文章 编号、创建人、创建时间、编辑人、编辑时间
		if (null == article.getPublish_time()) {
			article.setPublish_time(nowDate);
		}
		article.setCreator(userId);
		article.setCreate_time(nowDate);
		article.setEditor(userId);
		article.setEdit_time(nowDate);
		// 执行新增文章
		dao.insertArticle(article);

		// 反馈成功信息
		ajaxResponse.setSuccess(true);
		ajaxResponse.setSuccessMessage("文章新增成功！");
		return ajaxResponse;
	}

	/**
	 * 跳转到编辑文章页面
	 *
	 * @param articleId
	 *            文章编号
	 * @param model
	 *            反馈对象
	 * @param request
	 *            请求对象
	 * @return base + 'editAnnouncement'
	 * @throws Exception
	 *             抛出全部异常
	 */
	@RequestMapping(value = "/{articleId}/edit", method = RequestMethod.GET)
	public String editArticle(@PathVariable String articleId, Model model, HttpServletRequest request)
			throws Exception {
		// 获取文章信息
		Article article = dao.getArticleById(articleId);
		// 设置到反馈对象中
		model.addAttribute("article", article);
		// 执行跳转到编辑页面
		return basePath + "editArticle";
	}

	/**
	 * 执行编辑文章
	 *
	 * @param article
	 *            文章对象
	 * @param bindingResult
	 *            绑定对象
	 * @param request
	 *            请求对象
	 * @return ajax响应回执
	 * @throws Exception
	 *             抛出全部异常
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse editArticle(@Valid Article article, BindingResult bindingResult, HttpServletRequest request)
			throws Exception {
		// 定义AJAX响应回执
		AjaxResponse ajaxResponse = new AjaxResponse();
		// 如果发现错误则拼接错误信息执行回执
		if (bindingResult.hasErrors()) {
			ajaxResponse.bindingResultHandler(bindingResult);
			return ajaxResponse;
		}
		// 获取当前用户、当前时间
		SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER);
		String userId = sessionUser.getUser_id();
		Date nowDate = new Date();
		// 设置编辑人、编辑时间
		article.setEditor(userId);
		article.setEdit_time(nowDate);
		// 执行编辑文章
		dao.updateArticle(article);
		// 反馈成功信息
		ajaxResponse.setSuccess(true);
		ajaxResponse.setSuccessMessage("文章编辑成功！");
		// 用‘URL’字段通知前段页面的跳转路径
		Map<String, String> map = new HashMap<>();
		map.put("URL", basePath);
		ajaxResponse.setExtParam(map);
		return ajaxResponse;
	}

	/**
	 * 执行删除文章
	 *
	 * @param articleIds
	 *            文章编号列表
	 * @param request
	 *            请求对象
	 * @return ajax响应回执
	 * @throws Exception
	 *             抛出全部异常
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse deleteArticle(String articleIds, HttpServletRequest request) throws Exception {
		// 执行删除文章
		dao.deleteArticel(articleIds);
		// 执行响应回执
		AjaxResponse ajaxResponse = new AjaxResponse();
		ajaxResponse.setSuccess(true);
		ajaxResponse.setSuccessMessage("文章删除成功！");
		return ajaxResponse;
	}

	/**
	 * 执行启用文章
	 *
	 * @param articleIds
	 *            文章编号列表
	 * @param request
	 *            请求对象
	 * @return ajax响应回执
	 * @throws Exception
	 *             抛出全部异常
	 */
	@RequestMapping(value = "/open", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse openArticle(String articleIds, HttpServletRequest request) throws Exception {
		// 执行启用文章
		dao.openArticle(articleIds);
		// 执行响应回执
		AjaxResponse ajaxResponse = new AjaxResponse();
		ajaxResponse.setSuccess(true);
		ajaxResponse.setSuccessMessage("文章启用成功！");
		return ajaxResponse;
	}

	/**
	 * 执行禁用文章
	 *
	 * @param articleIds
	 *            文章编号列表
	 * @param request
	 *            请求对象
	 * @return ajax响应回执
	 * @throws Exception
	 *             抛出全部异常
	 */
	@RequestMapping(value = "/close", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse closeArticle(String articleIds, HttpServletRequest request) throws Exception {
		// 执行禁用文章
		dao.closeArticle(articleIds);
		// 执行响应回执
		AjaxResponse ajaxResponse = new AjaxResponse();
		ajaxResponse.setSuccess(true);
		ajaxResponse.setSuccessMessage("文章禁用成功！");
		return ajaxResponse;
	}

	/**
	 * 文章预览页面
	 *
	 * @param articleId
	 *            文章编号
	 * @param model
	 *            反馈对象
	 * @param request
	 *            请求对象
	 * @return base + 'editAnnouncement'
	 * @throws Exception
	 *             抛出全部异常
	 */
	@RequestMapping(value = "/{articleId}/check", method = RequestMethod.GET)
	public ModelAndView checkArticle(@PathVariable String articleId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView view = new ModelAndView("/wzgl/content/articleDetail.btl");
		String sourcePath = AttributeUtils.getAttributeContent(request.getServletContext(),
				"source_webapp_file_postion");
		// 获取文章信息
		Article article = dao.getArticleById(articleId);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		article.setEditor(sdf.format(article.getPublish_time()));
		view.addObject("article", article);
		view.addObject("SOURCEPATH", sourcePath);

		return view;
	}
}
